package example.posterity;

import com.scop.desensitize.cs.entity.User;
import com.scop.desensitize.encry.rreversible.symmetry.AbstractSymmetryEncry;
import com.scop.desensitize.exception.factory.FactoryException;
import com.scop.desensitize.exception.strategy.DecodeException;
import com.scop.desensitize.exception.strategy.EncodeException;
import com.scop.desensitize.factory.encry.EStrategy;
import com.scop.desensitize.factory.encry.irreversible.IrreverSibleFactory;
import com.scop.desensitize.factory.encry.rreversible.symmetry.SymmetryFactory;
import com.scop.desensitize.factory.posterity.ObjToJsonFactory;
import com.scop.desensitize.posterity.json.AbstractJsonPosterity;

/**
 * @desc 转Json数据脱敏功能拓展示例
 * @author gzy
 * @time 2022-05-01
 */
public class ObjToJsonPosterityDemo {
    public static void main(String[] args) throws FactoryException, EncodeException, DecodeException {
        defaultMethod();
//        method2();
    }



    private static void defaultMethod() throws EncodeException, FactoryException, DecodeException {
        AbstractJsonPosterity posterity = ObjToJsonFactory.create();
        User user=new User();
        user.setName("小明");
        String enStr = posterity.encode(user);
        Object deStr = posterity.decode(enStr, User.class);
        System.out.println("默认使用的是"+posterity.getEncry().getClass().getName()+"加密策略");
        System.out.println("加密："+enStr);
        System.out.println("解密："+deStr);

        System.out.println("\n\n以下我们手动设定一个加密策略");
        posterity.setEncry(IrreverSibleFactory.create(EStrategy.MD5));
        enStr = posterity.encode(user);
        System.out.println("使用的加密策略为："+posterity.getEncry().getClass().getName());
        System.out.println("加密："+enStr);
    }

    private static void method2() throws FactoryException, EncodeException, DecodeException {
        AbstractSymmetryEncry des3 = SymmetryFactory.create(EStrategy.DES3);

        System.out.println("通过工厂生产一个使用指定策略的字符串加密功能类");
        AbstractJsonPosterity posterity = ObjToJsonFactory.create(des3);
        User user=new User();
        user.setName("小明");
        String enStr = posterity.encode(user);
        Object deStr = posterity.decode(enStr, User.class);
        System.out.println("默认使用的是"+posterity.getEncry().getClass().getName()+"加密策略");
        System.out.println("加密："+enStr);
        System.out.println("解密："+deStr);
    }
}
